package com.uwlaser.rest;

import com.uwlaser.annotation.DateFormat;
import com.uwlaser.annotation.Log;
import com.uwlaser.domain.ChartResultEntity;
import com.uwlaser.domain.GetDataBySQLParam;
import com.uwlaser.domain.ResultTable;
import com.uwlaser.service.ChartDataService;
import com.uwlaser.service.dto.ChartDataSource;
import com.uwlaser.service.dto.GetChartDataSourceParam;
import com.uwlaser.service.dto.Source;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @description:图表数据源控制器
 * @author: UW_13724@caoguilin
 * @time: 2022/12/29 14:25
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "图表数据源接口")
@RequestMapping("/api/chartDataSource")
public class ChartDataSourceController {

    private final ChartDataService chartDataService;

    /**
     * 获取测试数据
     * @param param
     * @return
     */
    @GetMapping("/getTestData")
    @Log("获取测试数据")
    @ApiOperation("获取测试数据")
    public ChartResultEntity getTestData ( GetChartDataSourceParam param){

        ChartResultEntity data=new ChartResultEntity();
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM");
        try {
            Date date = dateFormat.parse(param.getYear()+"-"+param.getMonth());
            calendar.setTime(date);
            int actualMaximum = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
            data.setCode(actualMaximum);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return data;
    }

    /**
     * 获取某月的日产能
     * @param param
     * @return
     */
    @GetMapping("/getDailyOutput")
    @Log("获取某月的日产能")
    @ApiOperation("获取某月的日产能")
    public ResultTable getDailyOutput (GetChartDataSourceParam param){
        ResultTable resultTable = chartDataService.getOutput(param);
        return resultTable;
    }


    /**
     * 获取某年的月产能和合格率
     * @param param
     * @return
     */
    @GetMapping("/getAnnualOutput")
    @Log("获取某年的月产能")
    @ApiOperation("获取某年的月产能")
    public ResultTable getAnnualOutput (GetChartDataSourceParam param){
        ResultTable resultTable = chartDataService.getYearOutput(param);
        return resultTable;
    }


    /**
     * 获取某年某月的合格率
     * @param param
     * @return
     */
    @GetMapping("/getPassRate")
    @Log("获取某年的月产能")
    @ApiOperation("获取某年的月产能")
    public ResultTable getPassRate (GetChartDataSourceParam param){
        ResultTable resultTable = chartDataService.getPassRate(param);
        return resultTable;
    }

    /**
     * 获取当日产量
     * @return
     */
    @GetMapping("/getTodayOutput")
    @Log("获取当日产量")
    @ApiOperation("获取当日产量")
    public ResultTable getTodayOutput ( ){
        ResultTable resultTable = chartDataService.getTodayOutput();
        return resultTable;
    }

    /**
     * 获取当前生产的订单号
     * @return
     */
    @GetMapping("/getWorkOrder")
    @Log("获取当前生产的订单号")
    @ApiOperation("获取当前生产的订单号")
    public ResultTable getWorkOrder (GetChartDataSourceParam param){
        ResultTable resultTable = chartDataService.getWorkOrder(param);
        return resultTable;
    }

    /**
     * 获取当前生产的型号
     * @return
     */
    @GetMapping("/getProductCode")
    @Log("获取当前生产的型号")
    @ApiOperation("获取当前生产的型号")
    public ResultTable getProductCode (GetChartDataSourceParam param){
        ResultTable resultTable = chartDataService.getProductCode(param);
        return resultTable;
    }

    /**
     * 获取电芯档位
     * @return
     */
    @GetMapping("/getCellCapacity")
    @Log("获取电芯档位")
    @ApiOperation("获取电芯档位")
    public ResultTable getCellCapacity (GetChartDataSourceParam param){
        ResultTable resultTable = chartDataService.getCellCapacity(param);
        return resultTable;
    }

    /**
     * 接收SQL请求
     * @return
     */
    @PostMapping("/getDataBySQL")
    @Log("接收SQL请求")
    @ApiOperation("接收SQL请求")
    public ResultTable getDataBySQL (@RequestBody Map<String,String>map){
        ResultTable resultTable = chartDataService.queryDataBySQL(map.get("sql"));
        return resultTable;
    }


    /**
     * 获取当日往前N天的产量
     * @return
     */
    @GetMapping("/getBatchOutput")
    @Log("获取当日往前N天的产量")
    @ApiOperation("获取当日往前N天的产量")
    public ResultTable getBatchOutput (GetChartDataSourceParam param){
        ResultTable resultTable = chartDataService.queryBatchOutput(param);
        return resultTable;
    }

    /**
     * 获取当日往前N天的合格率
     * @return
     */
    @GetMapping("/getBatchRate")
    @Log("获取当日往前N天的合格率")
    @ApiOperation("获取当日往前N天的合格率")
    public ResultTable getBatchRate (GetChartDataSourceParam param){
        ResultTable resultTable = chartDataService.queryBatchRate(param);
        return resultTable;
    }
}
